import pdb

import xlrd, pdb, json

excel_data1 = r'D:\01_data\中医药四诊信息命名实体识别数据标注/unlabel_tcm_ner(1).xlsx'
excel_data2 = r'D:\01_data\中医药四诊信息命名实体识别数据标注/500-1000.xlsx'
out_path = './tcm_data/symptom.json'



def getjsonres(excel_data):

    data = xlrd.open_workbook(excel_data)
    tables = data.sheets()[0]

    # 注意后续可能需要修改的地方：头实体、尾实体定义，目前只分为部位+形容，可能后续对照实验为分四诊进行解析

    allres = []

    outdic = {
        'sentText':'',
        'relationMentions':[],
        'entityMentions':[]
    }

    nrows = tables.nrows

    relationMentions = []
    senttext = ''
    entityMentions = []
    entcount = 0

    for i in range(nrows):

        currow = tables.row_values(i)
        if currow[0].startswith('序号'):
            sentlis = []
            for char in currow[1:]:
                char = char.strip()
                if char != '':
                    sentlis.append(char)
                else:
                    break
            senttext = ''.join(sentlis)
        elif currow[0].startswith('望'):
            currow = currow[1:]

            entres = {}
            tmpres = {}
            tmpe1 = ''
            tmpe2 = ''
            for i, lab in enumerate(currow):
                lab = lab.strip()
                #pdb.set_trace()
                if lab == '' and tmpe1 != '' and tmpe2 != '':
                    tmpres['em1Text'] = tmpe1
                    tmpres['em2Text'] = tmpe2
                    tmpres['label'] = '/specificities/part/look'
                    entres = {'start':entcount, 'label': 'body', 'text':tmpe1}
                    entcount += 1
                    entityMentions.append(entres)
                    entres = {'start': entcount, 'label': 'description', 'text': tmpe2}
                    entcount += 1
                    entityMentions.append(entres)
                    relationMentions.append(tmpres)
                    tmpe1 = ''
                    tmpe2 = ''
                    tmpres = {}
                    entres = {}
                if lab.endswith('H') :
                    tmpstr = senttext[i]
                    tmpe1 += tmpstr
                elif lab.endswith('T'):
                    tmpstr = senttext[i]
                    tmpe2 += tmpstr

        elif currow[0].startswith('闻'):
            currow = currow[1:]
            entres = {}
            tmpres = {}
            tmpe1 = ''
            tmpe2 = ''
            for i, lab in enumerate(currow):
                lab = lab.strip()
                # pdb.set_trace()
                if lab == '' and tmpe1 != '' and tmpe2 != '':
                    tmpres['em1Text'] = tmpe1
                    tmpres['em2Text'] = tmpe2
                    entres = {'start':entcount, 'label': 'body', 'text':tmpe1}
                    entcount += 1
                    entityMentions.append(entres)
                    entres = {'start': entcount, 'label': 'description', 'text': tmpe2}
                    entcount += 1
                    entityMentions.append(entres)
                    tmpres['label'] = '/specificities/part/smell'
                    relationMentions.append(tmpres)
                    tmpe1 = ''
                    tmpe2 = ''
                    tmpres = {}
                    entres = {}
                if lab.endswith('H'):
                    tmpstr = senttext[i]
                    tmpe1 += tmpstr
                elif lab.endswith('T'):
                    tmpstr = senttext[i]
                    tmpe2 += tmpstr
        elif currow[0].startswith('问'):
            currow = currow[1:]
            tmpres = {}
            entres = {}
            tmpe1 = ''
            tmpe2 = ''
            for i, lab in enumerate(currow):
                lab = lab.strip()
                # pdb.set_trace()
                if lab == '' and tmpe1 != '' and tmpe2 != '':
                    tmpres['em1Text'] = tmpe1
                    tmpres['em2Text'] = tmpe2
                    tmpres['label'] = '/specificities/part/ask'
                    entres = {'start':entcount, 'label': 'body', 'text':tmpe1}
                    entcount += 1
                    entityMentions.append(entres)
                    entres = {'start': entcount, 'label': 'description', 'text': tmpe2}
                    entityMentions.append(entres)
                    entcount += 1
                    relationMentions.append(tmpres)
                    tmpe1 = ''
                    tmpe2 = ''
                    tmpres = {}
                    entres = {}

                if lab.endswith('H'):
                    tmpstr = senttext[i]
                    tmpe1 += tmpstr
                elif lab.endswith('T'):
                    tmpstr = senttext[i]
                    tmpe2 += tmpstr
        elif currow[0].startswith('切'):
            currow = currow[1:]
            tmpres = {}
            entres = {}
            tmpe1 = ''
            tmpe2 = ''
            for i, lab in enumerate(currow):
                lab = lab.strip()
                # pdb.set_trace()
                if lab == '' and tmpe1 != '' and tmpe2 != '':
                    tmpres['em1Text'] = tmpe1
                    tmpres['em2Text'] = tmpe2
                    entres = {'start':entcount, 'label': 'body', 'text':tmpe1}
                    entcount += 1
                    entityMentions.append(entres)
                    entres = {'start': entcount, 'label': 'description', 'text': tmpe2}
                    entcount += 1
                    entityMentions.append(entres)
                    tmpres['label'] = '/specificities/part/incision'
                    relationMentions.append(tmpres)
                    tmpe1 = ''
                    tmpe2 = ''
                    tmpres = {}
                    entres = {}
                if lab.endswith('H'):
                    tmpstr = senttext[i]
                    tmpe1 += tmpstr
                elif lab.endswith('T'):
                    tmpstr = senttext[i]
                    tmpe2 += tmpstr
        else:
            if relationMentions != [] and senttext != '' and entityMentions != '':

                outdic['sentText'] = senttext
                outdic['relationMentions'] = relationMentions
                outdic['entityMentions'] = entityMentions

                allres.append(outdic)

                relationMentions = []
                senttext = ''
                entityMentions = []
                entcount = 0
                outdic = {}
    return allres

res1 = getjsonres(excel_data1)
res2 = getjsonres(excel_data2)

#pdb.set_trace()
allres = res1 + res2

outfile = open(out_path, 'w', encoding='utf8')
for resli in allres:
    #print(resli)
    json.dump(resli, outfile, ensure_ascii=False)
    outfile.write('\n')
    #pdb.set_trace()
















